const { Send } = require("../utils/response");
const Notice = require("../models/notice");

class NoticeController {
    static async list(req, res, next) {
        try {
            const { pageno = 1, pagesize = 1000, keywords } = req.query;
            const regex = new RegExp(keywords, "i");
            const query = {
                $or: [
                    { title: { $regex: regex } },
                    { content: { $regex: regex } }
                ]
            };
            const counts = await Notice.countDocuments(query);
            const notices = await Notice.find(query)
                .select("+createdAt +isActive")
                .sort({
                    createdAt: -1
                })
                .skip((pageno - 1) * pagesize)
                .limit(pagesize);

            return Send(res, {
                data: {
                    pageno: parseInt(pageno),
                    pagesize: parseInt(pagesize),
                    total: counts,
                    list: notices
                }
            });
        } catch (error) {
            next(error);
        }
    }
    static async add(req, res, next) {
        try {
            const notice = await Notice.create({
                title: req.body.title,
                content: req.body.content
            });
            return Send(res, {
                data: {
                    _id: notice._id,
                    title: notice.title,
                    createdAt: notice.createdAt,
                    isActive: notice.isActive
                }
            });
        } catch (error) {
            next(error);
        }
    }
    static async detail(req, res, next) {
        try {
            const notice = await Notice.findOne({ _id: req.params.id }).select(
                "+createdAt +isActive +content"
            );
            if (!notice) {
                return Send(res, {
                    code: -1,
                    message: "公告不存在"
                });
            }
            return Send(res, {
                data: notice
            });
        } catch (error) {
            next(error);
        }
    }
    static async update(req, res, next) {
        try {
            if (!req.body) {
                return Send(res, {
                    code: -1,
                    message: "请填写要修改的参数"
                });
            }
            const notice = await Notice.findByIdAndUpdate(
                req.params.id,
                req.body
            );
            if (!notice) {
                return Send(res, {
                    code: -1,
                    message: "该公告不存在"
                });
            }
            Send(res, {
                message: "更新成功",
                data: notice._id
            });
        } catch (error) {
            next(error);
        }
    }
    static async delete(req, res, next) {
        try {
            const { deletedCount } = await Notice.deleteOne({
                _id: req.params.id
            });
            if (deletedCount === 0) {
                return Send(res, {
                    message: "该公告不存在"
                });
            }
            return Send(res, {
                message: "删除成功",
                data: req.params.id
            });
        } catch (error) {
            next(error);
        }
    }
    static async status(req, res, next) {
        try {
            const notice = await Notice.findOne({ _id: req.params.id }).select(
                "+isActive"
            );
            if (!notice) {
                return Send(res, {
                    code: -1,
                    message: "公告不存在"
                });
            }
            notice.isActive = !notice.isActive;
            await notice.save();
            return Send(res, {
                message: "更新成功",
                data: {
                    _id: notice._id,
                    isActive: notice.isActive
                }
            });
        } catch (error) {
            next(error);
        }
    }
}

module.exports = NoticeController;
